package com.example.deliveryapplication

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.example.deliveryapplication.entity.*
import com.example.deliveryapplication.sql.*


class NewPlanViewModel:BaseViewModel() {
    private val mClientResult = MutableLiveData<List<Client>>()
    val clientResult: LiveData<List<Client>> get() = mClientResult
    fun getAllClient(clientDao: ClientDao){
        launch {
            val clients = clientDao.loadAllChart()
            mClientResult.postValue(clients)
        }
    }

    private val mModelResult = MutableLiveData<List<BearingModel>>()
    val modelResult: LiveData<List<BearingModel>> get() = mModelResult
    fun getAllModel(clientDao: BearingModelDao){
        launch {
            val clients = clientDao.loadAllChart()
            mModelResult.postValue(clients)
        }
    }

    private val mInertPlanItemResult = MutableLiveData<Long>()
    val insertPlanItemResult: LiveData<Long> get() = mInertPlanItemResult
    fun insertPlanItem(planDao:PlanDao,planItemDao: PlanItemDao,bean:PlanItem){
        launch {
            val plan=planDao.loadPlanById(bean.planId)
            if(plan==null){
                val plan=Plan(bean.planId,"",0,handlerCurrentTimeToStandardString(System.currentTimeMillis()))
                planDao.insertChart(plan)
            }
            val result = planItemDao.insertChart(bean)
            mInertPlanItemResult.postValue(result)
        }
    }

    private val mPlanItemResult = MutableLiveData<List<PlanItemWithClient>>()
    val planItemResult: LiveData<List<PlanItemWithClient>> get() = mPlanItemResult
    fun getAllPlanItem(panItemDao: PlanItemDao,planId:String){
        launch {
            val clients = panItemDao.getPlanItemsWithClients(planId)
            mPlanItemResult.postValue(clients)
        }
    }
    private val mGetPlanResult = MutableLiveData<Plan>()
    val getPlanResult: LiveData<Plan> get() = mGetPlanResult
    fun getPlan(planDao: PlanDao,id:String){
        launch {
            val clients = planDao.loadPlanById(id)
            mGetPlanResult.postValue(clients)
        }
    }

    private val mInertPlanResult = MutableLiveData<Long>()
    val insertPlanResult: LiveData<Long> get() = mInertPlanResult
    fun insertPlan(planDao: PlanDao,bean:Plan){
        launch {
            val result = planDao.insertChart(bean)
            mInertPlanResult.postValue(result)
        }
    }

    private val mUpdatePlanResult = MutableLiveData<String>()
    val updatePlanResult: LiveData<String> get() = mUpdatePlanResult
    fun updatePlan(planDao: PlanDao,planId:String,type:Int,saveTime:String){
        launch {
            val bean = planDao.loadPlanById(planId)
           bean.type=type
            bean.time=saveTime
            planDao.updatePlan(bean)
            mUpdatePlanResult.postValue("")
        }
    }

    private val mCopyPlanResult = MutableLiveData<List<Long>>()
    val copyPlanResult: LiveData<List<Long>> get() = mCopyPlanResult
    fun copyPlan(planDao:PlanDao,planItemDao: PlanItemDao,id:String,newId:String){
        launch {
            val list = planItemDao.loadAllChart(id)

            list.forEach {
                it.planItemId= getNewUUid()
                it.planId=newId
            }
            val plan=planDao.loadPlanById(newId)
            if(plan==null){
                val plan=Plan(newId,"",0,handlerCurrentTimeToStandardString(System.currentTimeMillis()))
                planDao.insertChart(plan)
            }
            val result=planItemDao.insertPlanItems(list)
            mCopyPlanResult.postValue(result)
        }
    }
    private val mDeletePlanItemResult = MutableLiveData<Int>()
    val deletePlanItemResult: LiveData<Int> get() = mDeletePlanItemResult
    fun deletePlanItem(planItemDao: PlanItemDao,id:String){
        launch {
            val item=planItemDao.loadPlanItem(id)
            val result = planItemDao.deleteByPlanId(listOf(item))
            mDeletePlanItemResult.postValue(result)
        }
    }
    fun deletePlan(planDao: PlanDao,planId: String){
        Log.d("测试","ccccccccccccccccccccccccc")
        launch {
            val bean=planDao.loadPlanById(planId)
            planDao.deletePlan(bean)
        }
    }
    private val mCUResult = MutableLiveData<List<CU>>()
    val cuResult: LiveData<List<CU>> get() = mCUResult
    fun getAllCU(cuDao: CUDao){
        launch {
            val clients = cuDao.loadAllChart()
            mCUResult.postValue(clients)
        }
    }
}